import base from '@src/store/base'
import router from '@src/router'
import {
  storeBindingsBehavior
} from 'mobx-miniprogram-bindings'
import {
  queryProductList,
  getAvailablePoints
} from '@src/api/points';

Component({
  options: {
    addGlobalClass: true
  },
  behaviors: [storeBindingsBehavior, router.routerBehavior],
  storeBindings: [{
    store: base,
    fields: ['themeColor', 'themeTitleColor']
  }],
  data: {
    navHeight: 80,
    page: 1,
    pageNum: 10,
    hasNextPage: false,
    listData: {},
    accountInfo: {},
    loading: false
  },
  methods: {
    onShow() {
      this.initLoadingData();
    },
    // onLogin() {
    //   this.initLoadingData();
    // },
    onPullDownRefresh() {
      this.fetchAccount()
      wx.stopPullDownRefresh();
    },
    initLoadingData() {
      /* 初始化状态 */
      this.setData({
        listData: [],
        page: 1,
        pageSize: 10,
        hasNextPage: false
      })
      // 重新刷新数据
      this.fetchAccount()
      this.fetchPointsMallList()
    },
    fetchAccount() {
      getAvailablePoints().then(res => {
        this.setData({
          accountInfo: res.data
        })
      })
    },
    fetchPointsMallList() { // listData接口
      const data = {
        pageNum: this.data.page,
        pageSize: this.data.pageSize
      }
      queryProductList(data).then(res => {
        if (res.code === 0) {
          let list = res.data.list
          const hasNextPage = res.data.hasNextPage
          list = this.data.listData.concat(list)
          this.setData({
            listData: list,
            hasNextPage: hasNextPage
          })
        } else {
          // 异常处理
        }
      })
    },
    onReachBottom() {
      console.log('触底了')
      this.data.page = this.data.page + 1
      if (this.data.hasNextPage) { // 为true则刷新数据
        this.fetchPointsMallList()
      }
    }

  }
})
